/**
 * 
 */
package fr.toussna.service;

import java.util.List;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.Test;

import fr.toussna.entity.Article;
import fr.toussna.entity.Category;
import fr.toussna.entity.Comment;
import fr.toussna.entity.Member;
import fr.toussna.exception.ElementNotExistException;
import fr.toussna.utils.EntityFactory;

/**
 * Testing ArticleService
 * 
 * @author My
 * 
 */

@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@Test(suiteName = "ArticleServiceTestCase")
public class ArticleServiceTestCase extends AbstractTestNGSpringContextTests {

	private static final Log	LOGGER	= LogFactory
	                                           .getLog(ArticleServiceTestCase.class
	                                                   .getName());

	@Autowired
	@Qualifier("articleservice")
	private ArticleService	 articleService;

	@Autowired
	@Qualifier("entityfactory")
	private EntityFactory	 entityfactory;

	@Test
	public void addArticle() {
		LOGGER.info("addArticle - starting...");
		Article article = (Article) entityfactory.createDomainEntity("article");
		Article article2 = articleService.addArticle(article);
		Assert.assertNotNull(article2);
		Assert.assertEquals(article.getDate(), article2.getDate());
		Assert.assertEquals(article.getCategory().getName(), article2
		        .getCategory().getName());
		Assert.assertEquals(article.getMember().getEmail(), article2
		        .getMember().getEmail());
		Assert.assertEquals(article.getDate(), article2.getDate());
		Assert.assertEquals(article.getTitle(), article2.getTitle());
		articleService.deleteArticle(article2);
		LOGGER.info("addArticle - end.");
	}

	@Test
	public void findArticle() {
		LOGGER.info("findArticle - starting...");
		Article article = (Article) entityfactory.createDomainEntity("article");
		Article article2 = articleService.addArticle(article);
		Article article3;
		try {
			article3 = articleService.findArticle(article2.getId());

			Assert.assertNotNull(article3);
			Assert.assertEquals(article.getDate(), article3.getDate());
			Assert.assertEquals(article.getCategory().getName(), article3
			        .getCategory().getName());
			Assert.assertEquals(article.getMember().getEmail(), article3
			        .getMember().getEmail());
			Assert.assertEquals(article.getTitle(), article3.getTitle());
			articleService.deleteArticle(article3);
		} catch (ElementNotExistException e) {

		}
		LOGGER.info("findArticle - end.");
	}

	@Test
	public void updateArticle() {
		LOGGER.info("updateArticle - starting...");
		Article article = (Article) entityfactory.createDomainEntity("article");
		Article article2 = articleService.addArticle(article);
		article2.setDate("changed");
		article2.setContent("changed");
		article2.setTitle("changed");
		articleService.updateArticle(article2);
		Article article3;
		try {
			article3 = articleService.findArticle(article2.getId());
			Assert.assertNotNull(article3);
			Assert.assertEquals("changed", article3.getDate());
			Assert.assertEquals("changed", article3.getContent());
			Assert.assertEquals("changed", article3.getTitle());
			Assert.assertEquals("changed", article3.getTitle());
			articleService.deleteArticle(article2);
		} catch (ElementNotExistException e) {
		}
		LOGGER.info("updateArticle - end.");
	}

	@Test(expectedExceptions = { ElementNotExistException.class })
	public void deleteArticle() throws ElementNotExistException {
		LOGGER.info("deleteArticle - starting...");
		Article article = (Article) entityfactory.createDomainEntity("article");
		Article article2 = articleService.addArticle(article);
		articleService.deleteArticle(article2);
		articleService.findArticle(article2.getId());
		LOGGER.info("deleteArticle - end.");
	}

	@Test
	public void searchArticle() {
		LOGGER.info("searchArticle - starting...");
		Article article = (Article) entityfactory.createDomainEntity("article");
		Article article2 = articleService.addArticle(article);
		Assert.assertFalse(articleService.searchArticle(
		        article.getTitle().substring(0, 2)).isEmpty());
		Article art = (Article) entityfactory.createDomainEntity("article");
		Article art2 = articleService.addArticle(art);
		Assert.assertTrue(articleService.searchArticle(
		        article.getTitle().substring(0, 2)).size() == 2);
		Assert.assertTrue(articleService.searchArticle("hhehuhuxdhuehuheu33")
		        .size() == 0);
		articleService.deleteArticle(article2);
		articleService.deleteArticle(art2);
		LOGGER.info("searchArticle - end.");
	}

	@Test
	public void getCategoryOfArticle() {
		LOGGER.info("getCategoryOfArticle - starting...");
		Article article = (Article) entityfactory.createDomainEntity("article");
		Article art = (Article) entityfactory.createDomainEntity("article");
		Category category = (Category) entityfactory
		        .createDomainEntity("category");
		article.setCategory(category);
		art.setCategory(category);
		Article article2 = articleService.addArticle(article);
		Article art2 = articleService.addArticle(art);
		Assert.assertTrue(articleService.getCategoryOfArticle(category).size() == 2);
		articleService.deleteArticle(article2);
		articleService.deleteArticle(art2);
		LOGGER.info("getCategoryOfArticle - end.");
	}

	@Test
	public void findArticleOfMember() {
		LOGGER.info("findArticleOfMember - starting...");
		Article article = (Article) entityfactory.createDomainEntity("article");
		Article art = (Article) entityfactory.createDomainEntity("article");
		Member member = (Member) entityfactory.createDomainEntity("member");
		article.setMember(member);
		art.setMember(member);
		Article article2 = articleService.addArticle(article);
		Article art2 = articleService.addArticle(art);
		Assert.assertTrue(articleService.findArticleOfMember(member).size() == 2);
		articleService.deleteArticle(article2);
		articleService.deleteArticle(art2);
		LOGGER.info("findArticleOfMember - end.");
	}

	@Test
	public void addComment() {
		LOGGER.info("addComment - starting...");
		Article article = (Article) entityfactory.createDomainEntity("article");
		Article article2 = articleService.addArticle(article);
		Comment comment = (Comment) entityfactory.createDomainEntity("comment");
		comment.setArticle(article2);
		try {
			articleService.addComment(comment);
			Comment comment2 = articleService.findArticle(article2.getId()).getComments().get(0);
			Assert.assertEquals(comment.getTitle(), comment2.getTitle());
		} catch (ElementNotExistException e) {
		}
		articleService.deleteArticle(article2);
		LOGGER.info("addComment - end.");
	}

}
